package com.fourfinance.model.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fourfinance.model.dao.IpDAO;
import com.fourfinance.model.domain.Ip;

@Repository
public class IpDAOImpl implements IpDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void add(Ip ip) {
		sessionFactory.getCurrentSession().save(ip);
	}

	@Override
	public List<Ip> list() {
		return sessionFactory.getCurrentSession().createQuery("from Ip").list();
	}

	@Override
	public void remove(Integer id) {
		Ip ip = (Ip) sessionFactory.getCurrentSession().load(Ip.class, id);
		if (null != ip) {
			sessionFactory.getCurrentSession().delete(ip);
		}
	}

	@Override
	public Ip getByIpAddress(String ipAddress) {
		Query query = sessionFactory.getCurrentSession().createQuery("from Ip where ipAddress = :ipAddress");
		query.setParameter("ipAddress", ipAddress);
		return (Ip) query.uniqueResult();
	}

	@Override
	public void update(Ip ip) {
		sessionFactory.getCurrentSession().update(ip);
	}
	
	@Override
	public void removeAll() {
		sessionFactory.getCurrentSession().createQuery("delete from Ip");
	}	

}
